package com.chinasoft.mall.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.ImageView;

import com.chinasoft.mall.model.DownloadInfo;
import com.chinasoft.mall.model.FormFile;
import com.chinasoft.mall.widget.downloadimg.DownloadManageAys;

@SuppressLint({ "DrawAllocation", "DrawAllocation" })
public class NetImageView extends ImageView {
	private String localpath;
	private Paint paint;
	private String downloadurl;
	// 是否需要阴影加圆角
	private boolean isshadow = false;

	public void setIsshadow(boolean isshadow) {
		this.isshadow = isshadow;
	}

	public boolean isIsshadow() {
		return isshadow;
	}

	public String getDownloadurl() {
		return downloadurl;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (!isshadow) {
			super.onDraw(canvas);
		} else {
			Drawable drawable = getDrawable();
			if (drawable == null) {
				return;
			} else {
				canvas.save(Canvas.MATRIX_SAVE_FLAG);

				Rect rect = new Rect(2, 2, getMeasuredWidth() - 10,
						getMeasuredHeight() - 10);
				// 由于图片的实际尺寸比显示出来的图像要大一些，因此需要适当更改下大小，以达到较好的效果
				RectF rectF = new RectF(rect);
				canvas.drawRoundRect(rectF, 4f, 4f, paint);// 在原有矩形基础上，画成圆角矩形，同时带有阴影层。
				drawable.setBounds(0, 0, getMeasuredWidth() - 3,
						getMeasuredHeight() - 3);
				drawable.draw(canvas);// 此处为画原图像，由于canvas有层次效果，因此会盖在影子之上。
				canvas.restore();
			}
		}

	}

	public void setUploadpath(String uploadpath) {
		upload(uploadpath);
	}

	public void setFormFile(FormFile formFile) {
		upload(formFile);
	}

	/**
	 * 获取图片的本地地址
	 * 
	 * @return 若为 null 则没有上传本地图片
	 */
	public String getLocalpath() {
		return localpath;
	}

	public NetImageView(Context context) {
		super(context);
		paint = new Paint();// 初始化画笔，为后面阴影效果使用。
		paint.setAntiAlias(true);// 去除锯齿。
		paint.setShadowLayer(5f, 5.0f, 5.0f, Color.BLACK);// 设置阴影层，这是关键。
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	}

	public NetImageView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();// 初始化画笔，为后面阴影效果使用。
		paint.setAntiAlias(true);// 去除锯齿。
		paint.setShadowLayer(5f, 5.0f, 5.0f, Color.BLACK);// 设置阴影层，这是关键。
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
	}

	public void setDownloadurl(String downloadurl) {
		download(downloadurl);
	}

	// 上传本地
	private void upload(String uploadpath) {

	}

	// 上传拍照
	private void upload(FormFile formFile) {

	}

	// 下载
	private void download(String downloadurl) {
		downloadbySize(downloadurl, 0, 0);
	}

	/**
	 * 无圆角下载
	 * 
	 * @param downloadurl
	 */
	public void setDownload(String downloadurl) {
		downloadbySize(downloadurl, 0, 0, false);
	}

	// 下载
	public void downloadbySize(String downloadurl, int wide, int height) {
		Log.i("NetImageView", downloadurl);
		this.downloadurl = downloadurl;
		// setImageResource(R.drawable.icon);
		if (downloadurl == null || "".equals(downloadurl)) {
			return;
		}
		DownloadInfo _dli = new DownloadInfo(downloadurl, this, true);
		_dli.setBitmapWH(wide, height);
		DownloadManageAys.getInstance().startDownload(_dli);
	}

	public void downloadbySize(String downloadurl, int wide, int height,
			boolean isCircle) {
		Log.i("NetImageView", downloadurl);
		this.downloadurl = downloadurl;
		// setImageResource(R.drawable.icon);
		if (downloadurl == null || "".equals(downloadurl)) {
			return;
		}
		DownloadInfo _dli = new DownloadInfo(downloadurl, this, isCircle);
		_dli.setBitmapWH(wide, height);
		DownloadManageAys.getInstance().startDownload(_dli);
	}

	public void downloadbySize(String downloadurl, int wide, int height,
			boolean isCircle, boolean isShadow) {
		Log.i("NetImageView", downloadurl);
		this.downloadurl = downloadurl;
		// setImageResource(R.drawable.icon);
		if (downloadurl == null || "".equals(downloadurl)) {
			return;
		}
		DownloadInfo _dli = new DownloadInfo(downloadurl, this, isCircle);
		_dli.setShadow(isShadow);
		_dli.setBitmapWH(wide, height);
		DownloadManageAys.getInstance().startDownload(_dli);
	}
	
	public void downloadbySize(String downloadurl, int wide, int height,
			boolean isCircle, boolean isShadow,ImageViewCallback imvb) {
		Log.i("NetImageView", downloadurl);
		this.downloadurl = downloadurl;
		// setImageResource(R.drawable.icon);
		if (downloadurl == null || "".equals(downloadurl)) {
			return;
		}
		DownloadInfo _dli = new DownloadInfo(downloadurl, this, isCircle,imvb);
		_dli.setShadow(isShadow);
		_dli.setBitmapWH(wide, height);
		DownloadManageAys.getInstance().startDownload(_dli);
	}
	
	public interface ImageViewCallback{
		public Drawable beforeDraw(Drawable drawable);
	}
}